package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.dto.machine.CreateOrUpdateAswDTO;
import io.github.talelin.latticy.mapper.AswMapper;
import io.github.talelin.latticy.model.AswDO;
import io.github.talelin.latticy.model.AswItemDO;
import io.github.talelin.latticy.service.AswService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-05-28
 */
@Service
public class AswServiceImpl extends ServiceImpl<AswMapper, AswDO> implements AswService {


    @Autowired
    private AswMapper aswMapper;

    @Override
    public List<AswDO> findAll() {
        List<AswDO> asws = aswMapper.selectList(null);
        return asws;
    }

    @Override
    public List<Map<String, Object>> fingAllAsw() {
        return aswMapper.fingAllAsw();
    }

    @Override
    public List<AswItemDO> fingAllAswItemByID(Integer AswItemID) {
        return aswMapper.fingAllAswItemByID(AswItemID);
    }
    @Override
    public List<Map<String, Object>> findAswByID(Integer ID) {
        return aswMapper.findAswByID(ID);
    }

    @Override
    public AswDO getAswById(Integer id) {
        return aswMapper.selectById(id);
    }

    @Override
    public Integer deleteAswItemByID(Integer id) {
        return aswMapper.deleteAswItemByID(id);
    }
    public String selectNameBySortID(Integer sortID){
        return aswMapper.selectNameBySortID(sortID);
    };
    @Override
    public boolean deleteById(Integer id) {
        return aswMapper.deleteById(id) > 0;
    }

    @Override
    public boolean createAsw(CreateOrUpdateAswDTO validator) {
        AswDO aswDO = new AswDO();
        aswDO.setChcktype(validator.getChcktype());
        aswDO.setAswtype(validator.getAswtype());
        aswDO.setAswtitle(validator.getAswtitle());
        aswDO.setTrueasw(validator.getTrueasw());
        aswDO.setAnalysistxt(validator.getAnalysistxt());
        aswDO.setSortId(validator.getSortid());
        int res = aswMapper.insert(aswDO);
        if(res>0){
            ArrayList<Map<String, Object>> list = new ArrayList<>();
            list = (ArrayList<Map<String, Object>>) validator.getDynamicitem();
            System.out.println("list======="+list);
            for (Map map:list) {
                map.put("aswItemID",aswMapper.findMaxId());
                map.put("value",map.get("name"));
                aswMapper.insertAswItem(map);
            }
        }
        return res > 0;
    }

    public Integer findMaxId(){
        return aswMapper.findMaxId();
    }

    public Integer insertAswItem(Map<String,Object> map){
        return aswMapper.insertAswItem(map);
    }

}
